package com.ruoyi.web.controller.system;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.domain.DTO.BuyDto;
import com.ruoyi.system.domain.Orders;
import com.ruoyi.system.service.IOrdersService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.PetSupply;
import com.ruoyi.system.service.IPetSupplyService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 宠物用品信息表Controller
 * 
 * @author 李姝函
 * @date 2024-09-14
 */
@Controller
@RequestMapping("/system/supply")
public class PetSupplyController extends BaseController
{
    private String prefix = "system/supply";

    @Resource
    private IOrdersService ordersService;

    @Autowired
    private IPetSupplyService petSupplyService;

    @RequiresPermissions("system:supply:view")
    @GetMapping()
    public String supply()
    {
        return prefix + "/supply";
    }

    /**
     * 查询宠物用品信息表列表
     */
//    @RequiresPermissions("system:supply:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(PetSupply petSupply)
    {
        startPage();
        List<PetSupply> list = petSupplyService.selectPetSupplyList(petSupply);
        return getDataTable(list);
    }
    @PostMapping("/buy")
    @ResponseBody
    public AjaxResult buy(BuyDto buyDto){
        try{
//         获取服务id
            String supplyId = buyDto.getSupplyId();
//        获取数量
            String quantity = buyDto.getQuantity();

//        查询服务项目信息
            PetSupply petSupply = petSupplyService.selectPetSupplyById(Long.parseLong(supplyId));
//        计算消费金额
            BigDecimal amount = petSupply.getPrice().multiply(new BigDecimal(quantity));
            Orders orders = new Orders();
            orders.setAmount(amount);
            orders.setSupplyId(Long.parseLong(supplyId));
            orders.setUserId(ShiroUtils.getUserId());
            int i = ordersService.insertOrders(orders);
//            扣减库存
            petSupply.setStockQuantity(petSupply.getStockQuantity()-Integer.parseInt(quantity));
            petSupplyService.updatePetSupply(petSupply);
            return AjaxResult.success("购买成功");
        }catch (Exception e){
            return AjaxResult.error("购买时发生异常，请联系工作人员【000-111-00】");
        }
    }

    /**
     * 导出宠物用品信息表列表
     */
    @RequiresPermissions("system:supply:export")
    @Log(title = "宠物用品信息表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(PetSupply petSupply)
    {
        List<PetSupply> list = petSupplyService.selectPetSupplyList(petSupply);
        ExcelUtil<PetSupply> util = new ExcelUtil<PetSupply>(PetSupply.class);
        return util.exportExcel(list, "宠物用品信息表数据");
    }

    /**
     * 新增宠物用品信息表
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存宠物用品信息表
     */
    @RequiresPermissions("system:supply:add")
    @Log(title = "宠物用品信息表", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(PetSupply petSupply)
    {
        SysUser sysUser = ShiroUtils.getSysUser();
        petSupply.setCreateBy(sysUser.getUserName());
        petSupply.setUpdateBy(sysUser.getUserName());
        return toAjax(petSupplyService.insertPetSupply(petSupply));
    }

    /**
     * 修改宠物用品信息表
     */
    @RequiresPermissions("system:supply:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        PetSupply petSupply = petSupplyService.selectPetSupplyById(id);
        mmap.put("petSupply", petSupply);
        return prefix + "/edit";
    }

    /**
     * 修改保存宠物用品信息表
     */
    @RequiresPermissions("system:supply:edit")
    @Log(title = "宠物用品信息表", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PetSupply petSupply)
    {
        SysUser sysUser = ShiroUtils.getSysUser();
        petSupply.setUpdateBy(sysUser.getUserName());
        return toAjax(petSupplyService.updatePetSupply(petSupply));
    }

    /**
     * 删除宠物用品信息表
     */
    @RequiresPermissions("system:supply:remove")
    @Log(title = "宠物用品信息表", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(petSupplyService.deletePetSupplyByIds(ids));
    }
}
